﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css" />
    <link rel="stylesheet" href="/assets/module/admin.css">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
    </style>
</head>
<body>

    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 表格工具栏 -->
                <form class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">部门名称:</label>
                            <div class="layui-input-inline">
                                <input name="DepartmentName" class="layui-input" placeholder="输入部门名称" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            &emsp;
                            <button type="button" class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit>
                                <i class="layui-icon">&#xe615;</i>搜索
                            </button>&nbsp;
                            <button type="button" id="authoritiesAddBtn" class="layui-btn icon-btn" type="button">
                                <i class="layui-icon">&#xe654;</i>添加
                            </button>
                        </div>
                    </div>
                </form>
                <!-- 数据表格 -->
                <table id="dataTable"></table>
            </div>
        </div>
    </div>

    <!-- 表格操作列 -->
    <script type="text/html" id="TbBar">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <!-- 表单弹窗 -->
    <script type="text/html" id="authoritiesEditDialog">
        <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
              style="padding-right: 20px;">
            <input name="authorityId" type="hidden" />
            <div class="layui-row">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">上级部门</label>
                        <div class="layui-input-block">
                            <div id="ParentId" class="ew-xmselect-tree"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label layui-form-required">部门名称:</label>
                        <div class="layui-input-block">
                            <input name="DepartmentName" placeholder="请输入部门名称" class="layui-input"
                                   lay-verType="tips" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label layui-form-required">负责人:</label>
                        <div class="layui-input-block">
                            <select name="PrincipalId" id="PrincipalId" lay-verType="tips" lay-verify="required" 
                                    lay-filter="PrincipalId" required></select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话号码:</label>
                        <div class="layui-input-block">
                            <input name="Telephone" placeholder="请输入电话号码" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">传真:</label>
                        <div class="layui-input-block">
                            <input name="Fax" placeholder="请输入传真" class="layui-input" />
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">Email:</label>
                        <div class="layui-input-block">
                            <input name="Email" placeholder="请输入Email" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">显示顺序:</label>
                        <div class="layui-input-block">
                            <input name="DepartmentSort" placeholder="" class="layui-input" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </form>
    </script>

    <!-- js部分 -->
    <script src="/assets/libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="/assets/libs/layui/layui.js"></script>
    <script src="/assets/js/utils.js"></script>
    <script src="/assets/js/main.js"></script>

    <script>
        layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect','formX'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var admin = layui.admin;
            var treeTable = layui.treeTable;
            var util = layui.util;
            var xmSelect = layui.xmSelect;
            var formX = layui.formX;

            var tbDataList = [];

            // 渲染表格
            var insTb = treeTable.render({
                elem: '#dataTable',
                tree: {
                    iconIndex: 1,
                    idName: 'Id',
                    pidName: 'ParentId',
                    isPidData: true,
                    openName: 'DepartmentName',   // 自定义默认展开的字段名
                },
              
                cellMinWidth: 100,
                cols: [
                    { type: 'numbers' },
                    { field: 'DepartmentName', title: '部门名称', width: '20%' },
                    { field: 'DepartmentSort', title: '显示顺序', width: '10%', align: "left" },
                    { field: 'PrincipalName', title: '负责人', width: '10%', align: "left" },
                    {
                        field: 'BaseModifyTime', title: '创建时间', width: '20%', align: "left",
                        formatter: function (value, row, index) {
                            return ys.formatDate(value, "yyyy-MM-dd HH:mm:ss");
                        }
                    },
                    { title: '操作', templet: '#TbBar', singleLine: false, align: 'center', width: 120 }
                ],
                reqData: function (data, callback) {
                    admin.req('/OrganizationManage/Department/GetListJson', function (res) {
                        if (res.Tag == 1) {
                            tbDataList = res.Data;
                        } else {
                            tbDataList = [];
                        }
                        callback(tbDataList);
                    }, 'get');
                }
            });

            /* 表格操作列点击事件 */
            treeTable.on('tool(dataTable)', function (obj) {

                if (obj.event === 'edit') { // 修改
                    showEditModel(obj.data);
                } else if (obj.event === 'del') { // 删除
                    doDel(obj);
                }
            });

            /* 表格搜索 */
            form.on('submit(authoritiesTbSearch)', function (data) {
                doTbSearch(data.field, 'Id');
                return false;
            });

            /* 添加 */
            $('#authoritiesAddBtn').click(function () {
                showEditModel();
            });

            var ChangeId = 0;
            /* 显示表单弹窗 */
            function showEditModel(mData) {
                admin.open({
                    type: 1,
                    area: '600px',
                    title: (mData ? '修改' : '添加') + '权限',
                    content: $('#authoritiesEditDialog').html(),
                    success: function (layero, dIndex) {

                        var loadIndex = layer.load(2);


                        // 赋值Id
                        ChangeId = mData ? mData.Id : 0;

                        // 表单提交事件
                        form.on('submit(authoritiesEditSubmit)', function (data) {
                            data.field.ParentId = insXmSel.getValue('valueStr');
                            data.field.Id = ChangeId;
                            var loadIndex = layer.load(2);
                            admin.req('/OrganizationManage/Department/SaveFormJson', data.field, function (res) {
                                layer.close(loadIndex);
                                if (res.Tag == 1) {
                                    layer.close(dIndex);
                                    layer.msg("操作成功", { icon: 1 });
                                    insTb.refresh();
                                } else {
                                    layer.msg(res.Message, { icon: 2 });
                                }
                            },'post');
                            return false;
                        });

                        // 渲染下拉树
                        var insXmSel = xmSelect.render({
                            el: '#ParentId',
                            height: '250px',
                            data: insTb.options.data,
                            initValue: mData ? [mData.ParentId] : [],
                            model: { label: { type: 'text' } },
                            prop: {
                                name: 'DepartmentName',
                                value: 'Id'
                            },
                            radio: true,
                            clickClose: true,
                            tree: {
                                show: true,
                                indent: 15,
                                strict: false,
                                expandedKeys: true
                            }
                        });

                        admin.req('/OrganizationManage/Department/GetUserListJson', function (res) {
                            layer.close(loadIndex);
                            if (res.Tag == 1) {
                                

                                // 渲染搜索栏中的性别select
                                formX.renderSelect({
                                    elem: '#PrincipalId',
                                    data: res.Data,
                                    name: 'RealName',
                                    value: 'Id',
                                    hint: '请选择',
                                    initValue: mData ?mData.PrincipalId:0
                                });
                            } else {
                                layer.msg(res.Message, { icon: 2 });
                            }
                        });

                        // 弹窗不出现滚动条
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');


                        // 回显表单数据
                        form.val('authoritiesEditForm', mData);
                    }
                });
            }

            /* 删除 */
            function doDel(obj) {
                layer.confirm('确定要删除吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.close(i);
                        var loadIndex = layer.load(2);
                        admin.req('/OrganizationManage/Department/DeleteFormJson', obj.data.Id, function (res) {
                        layer.close(loadIndex);
                        if (res.Tag == 1) {
                            layer.msg("操作成功", { icon: 1 });
                            insTb.refresh();
                        } else {
                            layer.msg(res.Message, { icon: 2 });
                        }
                    },'post');
                });
            }

            // 搜索表格数据
            function doTbSearch(field, idName) {
                var ids = [], isClear = true;
                for (var i = 0; i < tbDataList.length; i++) {
                    var item = tbDataList[i], flag = true;
                    for (var f in field) {
                        if (!field.hasOwnProperty(f)) continue;
                        if (!field[f]) continue;
                        isClear = false;
                        if (!item[f] || (item[f] + "").indexOf(field[f]) === -1) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) ids.push(item[idName]);
                }
                if (isClear) {
                    insTb.clearFilter();
                } else {
                    insTb.filterData(ids);
                }
            }

            form.render('select');
        });
    </script>
</body>
</html>
